package attribute

import (
	"context"
	// "fmt"
	"strconv"
	"strings"

	"mall/common/utils/pointy"
	"mall/service/backend/api/internal/logic/product/util"
	"mall/service/backend/api/internal/logic/utils/parse"
	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/common/i18n"
	"mall/service/product/rpc/productclient"

	// "github.com/sirupsen/logrus"
	"github.com/zeromicro/go-zero/core/logx"
)

type GetAttributeListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetAttributeListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAttributeListLogic {
	return &GetAttributeListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetAttributeListLogic) GetAttributeList(req *types.AttributeListReq) (*types.AttributeListResp, error) {
	// logrus.Info(fmt.Sprintf("GetAttributeList: %+v", req))

	page, pageSize := parse.ParsePageInfo(
		req.Current,
		req.PageSize,
		l.svcCtx.Config.Query.DefaultAmount,
		l.svcCtx.Config.Query.MaxAmount,
	)

	orderBys := util.ParseSorterNew(req.Sorter)
	filterBys := util.ParseFilterNew(req.Filter, req.Type)

	var ids string
	var search *string
	if req.Search != nil && len(strings.TrimSpace(*req.Search)) > 0 { // 前端可以搜名称或者ID
		reqSearch := strings.TrimSpace(*req.Search)
		if _, err := strconv.ParseInt(reqSearch, 10, 64); err == nil {
			ids = reqSearch
		} else {
			search = pointy.GetPointer(reqSearch)
		}
	}
	var name *string
	if req.Name != nil && len(strings.TrimSpace(*req.Name)) > 0 {
		name = pointy.GetPointer(strings.TrimSpace(*req.Name))
	}
	var slug *string
	if req.Slug != nil && len(strings.TrimSpace(*req.Slug)) > 0 {
		slug = pointy.GetPointer(strings.TrimSpace(*req.Slug))
	}

	if req.Id != nil && *req.Id > 0 {
		ids = strconv.FormatInt(int64(*req.Id), 10)
	}
	if req.Ids != nil && len(strings.TrimSpace(*req.Ids)) > 0 {
		ids = strings.TrimSpace(*req.Ids)
	}

	data, err := l.svcCtx.ProductRpc.BackendGetAttributeList(l.ctx, &productclient.AttributeListReq{
		Page:        page,
		PageSize:    pageSize,
		Ids:         pointy.GetPointer(ids),
		Name:        name,
		Slug:        slug,
		TermId:      req.TermId,
		Taxonomy:    req.Taxonomy,
		IsVariation: req.IsVariation,
		Status:      req.Status,
		Search:      search,
		StartTime:   req.StartTime,
		EndTime:     req.EndTime,
		OrderBy:     orderBys,
		FilterBy:    filterBys,
		WithOption:  req.WithOption,
	})
	if err != nil {
		return nil, err
	}

	resp := &types.AttributeListResp{
		BaseDataInfo: types.BaseDataInfo{
			Success: true,
			Msg:     l.svcCtx.Trans.Trans(l.ctx, i18n.Success),
		},
		Data: types.AttributeListInfo{
			BaseListInfo: types.BaseListInfo{
				Current:  page,
				PageSize: pageSize,
				Total:    data.GetTotal(),
			},
			StatusStatisticMap: data.StatusStatisticMap,
		},
	}

	for _, v := range data.List {
		var opts []*types.AttributeOptionInfo
		if len(v.Options) > 0 {
			for _, v := range v.Options {
				opts = append(opts, &types.AttributeOptionInfo{
					BaseIDInfo: types.BaseIDInfo{
						Id:        v.Id,
						CreatedAt: v.CreatedAt,
					},
					AttributeId: v.AttributeId,
					Name:        v.Name,
					Slug:        v.Slug,
					Sort:        v.Sort,
					Status:      v.Status,
				})
			}
		}

		option := util.GetOptionByStatusAndId(v.Status, 0)
		resp.Data.List = append(resp.Data.List, &types.AttributeInfo{
			BaseIDInfo: types.BaseIDInfo{
				Id:         v.Id,
				CreatedAt:  v.CreatedAt,
				UpdatedAt:  v.UpdatedAt,
				DeletedAt:  v.DeletedAt,
				CanAccept:  option.CanAccept,
				CanReject:  option.CanReject,
				CanRemove:  option.CanRemove,
				CanRestore: option.CanRestore,
			},
			TermId:      v.TermId,
			Taxonomy:    v.Taxonomy,
			IsVariation: v.IsVariation,
			Name:        v.Name,
			Slug:        v.Slug,
			HasImage:    v.HasMedia,
			Sort:        v.Sort,
			Status:      v.Status,
			Options:     opts,
		})
	}

	return resp, nil
}
